package com.fdd.furns.web;

import com.fdd.furns.pojo.Member;
import com.fdd.furns.service.MemberService;
import com.fdd.furns.service.impl.MemberServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet(urlPatterns = "/memberServlet")
public class MemberServlet extends BasicServlet{
    private MemberService memberService = new MemberServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //调用memberService.login()
        Member loginMember = new Member(0, username, password, null);
        //通过用户名和密码判断用户是否存在
        Member member = memberService.login(loginMember);

        if (member != null) {
            //放到session中，欢迎页面需要获取用户名
            req.getSession().setAttribute("member",member);
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        } else {
            System.out.println("登录失败");
            req.setAttribute("error","用户名或密码错误");
            //回显用户名
            req.setAttribute("username",username);
            //页面转发
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        //用户提交的验证码
        String code = req.getParameter("code");


        //获取session中的验证码
        String cookie = (String)req.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        //立即删除session中的验证码，验证码使用一次后就应该失效，否则可能会被重复利用
        req.getSession().removeAttribute("KAPTCHA_SESSION_KEY");

        //判断验证码是否正确
        if(null != cookie && code.equalsIgnoreCase(cookie)){
            if (!memberService.isExist(username)) {//用户名不存在，可以注册
                Member member = new Member(0, username, password, email);
                if (memberService.registerMember(member)) {
                    req.getRequestDispatcher("/views/member/register_ok.jsp").forward(req, resp);
                } else {
                    req.getRequestDispatcher("/views/member/register_fail.jsp").forward(req, resp);
                }
            } else {//用户名存在，不能注册
//                System.out.println("用户名存在，不能注册");
                req.setAttribute("error", "用户名已存在，请重新注册");
                req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            }
        }else{
            System.out.println("验证码错误");
            req.setAttribute("error","验证码错误");
            req.setAttribute("active","register");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }



    }
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }

    //判断用户名是否存在,返回json数据
    protected void isExist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("进入了isExist方法");
        String username = req.getParameter("username");
//        System.out.println("username = " + username);
        boolean exist = memberService.isExist(username);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("exist",exist);
        String resultJson = new Gson().toJson(resultMap);
        resp.getWriter().write(resultJson);
    }
}
